/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 9:07:00
 * kws
 */

package com.crayon.account.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.crayon.account.dao.DAOAccount;
import com.crayon.account.data.ENMapKey;
import com.crayon.account.data.ItemGroup;
import com.crayon.account.fragments.ui.FragmentPieChart.OnPieChartDataBinder;
import com.crayon.account.fragments.ui.FragmentTopSum;
import com.crayon.account.item.adapter.AdapterAccountExpandable;
import com.crayon.sharemenu.R;
import com.crayon.util.GDB;
import com.crayon.util.UTILSC;

import org.afree.chart.AFreeChart;
import org.afree.data.general.PieDataset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

abstract public class FragmentChildChart extends FragmentChildRoot 
implements 
OnClickListener
,OnPieChartDataBinder
{

	abstract public boolean onCreateChartSingle(List<ItemGroup> groups, Fragment fragment, int resId);
	abstract public boolean onCreateChartIncom(List<ItemGroup> groups, Fragment fragment, int resId);
	abstract public boolean onCreateChartOut(List<ItemGroup> groups, Fragment fragment, int resId);
	abstract public String onCreateChartKey(DAOAccount item);
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_child_piechart,container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		FragmentTransaction tr = getChildFragmentManager().beginTransaction();
		tr.replace(R.id.fragment_top_sum, FragmentTopSum.onCreateInstance(getmDate().getTime()));
		tr.commit();
		
		ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progress_loading);
		progressBar.setVisibility(View.VISIBLE);
		OnReLoad();

	}


	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onLoadFinished(Loader<HashMap<ENMapKey, Object>> arg0,
			HashMap<ENMapKey, Object> arg1) {
		// TODO Auto-generated method stub
		super.onLoadFinished(arg0, arg1);
		hashMap.clear();
		groupDatas.clear();
		childData.clear();

		super.onLoadFinished(arg0, arg1);
		List<DAOAccount> daoAccounts = (List)arg1.get(ENMapKey.DAOLISTARRAY);
		long totalSumTmpIncom =0;
		long totalSumTmpOutCom =0;
		long totalSumTmpAll =0;
		
		for(DAOAccount item : daoAccounts){
			String key=onCreateChartKey(item);

			LinkedList<DAOAccount> list =  new LinkedList<DAOAccount>();
			if(hashMap.containsKey(key)) {
				list =	hashMap.get(key);
			}else{
				hashMap.put(key, list);
			}
			if(UTILSC.isINCOMECheckIOType(item)){
				totalSumTmpIncom += item.getXprice();
			}else{
				totalSumTmpOutCom+= item.getXprice();
			}
			totalSumTmpAll+=item.getXprice();
			list.add(item);
		}

		List<String> sortedKeys = new ArrayList(hashMap.keySet());
		Collections.sort(sortedKeys, Collections.reverseOrder()); 
		ArrayList<ItemGroup> groupListIn =new ArrayList<ItemGroup>();
		ArrayList<ItemGroup> groupListOut =new ArrayList<ItemGroup>();
		ArrayList<ItemGroup> groupListAll =new ArrayList<ItemGroup>();
		for (String key : sortedKeys) {
			ItemGroup itemGin =new ItemGroup();
			ItemGroup itemGOut =new ItemGroup();
			ItemGroup itemAll = new ItemGroup();
			
			itemGin.setKey(key);
			itemGOut.setKey(key);
			itemAll.setKey(key);
			List<Map<String,DAOAccount>> child =new ArrayList<Map<String,DAOAccount>>();
			int childCount=0;
			for(DAOAccount item: hashMap.get(key)){
				if(UTILSC.isINCOMECheckIOType(item)){
					itemGin.setChildCount(itemGin.getChildCount()+1);
					itemGin.setPriceTotal(itemGin.getPriceTotal() + item.getXprice());

				}else {
					itemGOut.setChildCount(itemGOut.getChildCount()+1);
					itemGOut.setPriceTotal(itemGOut.getPriceTotal() + item.getXprice());
				}
				itemAll.setChildCount(itemAll.getChildCount()+1);
				itemAll.setPriceTotal(itemAll.getPriceTotal()+item.getXprice());
				
				Map<String, DAOAccount> map = new HashMap<String, DAOAccount>(); 
				map.put(key, item);
				child.add(map);

			}
			if(itemGin.getPriceTotal()>0){
				long totalpercent = Math.round((100*itemGin.getPriceTotal())/totalSumTmpIncom);
				itemGin.setTotalpercent(totalpercent);
				if(itemGin.getChildCount()>0)groupListIn.add(itemGin);
				
				
				GDB.log(getClass(), " totalSumTmpIncom " + totalSumTmpIncom +"\ttotalpercent" + totalpercent +"\t groupListIn " + groupListIn);

			}
			if(itemGOut.getPriceTotal()>0){
				long totalpercent = Math.round(100*itemGOut.getPriceTotal()/totalSumTmpOutCom);
				itemGOut.setTotalpercent(totalpercent);
				if(itemGOut.getChildCount()>0)groupListOut.add(itemGOut);
			}
			if(itemAll.getPriceTotal()>0){
				long totalpercent = Math.round((100*itemAll.getPriceTotal())/totalSumTmpAll);
				itemAll.setTotalpercent(totalpercent);
				if(itemAll.getChildCount()>0)groupListAll.add(itemAll);
			}
			HashMap<String,ItemGroup> groupItem =new HashMap<String, ItemGroup>();
			groupItem.put(AdapterAccountExpandable.NAME_GROUP,itemGin );
			childData.add(child);
			groupDatas.add(groupItem);

		}
		//			
		onCreateChartOut(groupListOut, this, R.id.fragment_piechart);
		onCreateChartIncom(groupListIn,this,R.id.fragment_piechart_second);
		onCreateChartSingle(groupListAll,this, R.id.fragment_piechart);
		
	}

	@Override
	public Loader<HashMap<ENMapKey, Object>> onCreateLoader(int arg0,
			Bundle arg1) {
		// TODO Auto-generated method stub
		ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progress_loading);
		progressBar.setVisibility(View.VISIBLE);
		return super.onCreateLoader(arg0, arg1);
	}

	@Override
	public PieDataset OnPieDataSet() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AFreeChart createChart(PieDataset dataset) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
